use crate::{
    helpers::{Bot, Config},
    keyboards::standart_keyboard,
};
use anyhow::Result;
use db::{Db /*models::MatrixOrTelegramId*/};
use teloxide::prelude::*;
use uuid::Uuid;

pub async fn unarchive(
    bot: Bot,
    q: CallbackQuery,
    data: &str,
    db: Db,
    _config: Config,
) -> Result<()> {
    let _user_id = q.from.id;
    // let user = MatrixOrTelegramId::TelegramId { id: user_id, username: None };
    let post_id = Uuid::parse_str(data)?;
    // let user = db.get_user_id(&user).await?;
    db.unarchive_post(post_id).await?;
    let is_favorite = db.is_favorite(&post_id).await?;
    let is_real = db.is_read(&post_id).await?;
    let message_id = q.message.clone().unwrap().id;
    let chat_id = q.message.clone().unwrap().chat.id;
    // let text = escape(&escape_link_url(&clear_label(q.message.clone().unwrap().text().unwrap().to_string())));
    bot.answer_callback_query(q.id).text("Unarchived!").await?;
    // bot.edit_message_text(chat_id, message_id, text)
    //     .entities(q.message.clone().unwrap().entities().unwrap().to_owned())
    bot.edit_message_reply_markup(chat_id, message_id)
        .reply_markup(standart_keyboard(data, false, is_favorite, is_real))
        .await?;
    Ok(())
}

fn clear_label(text: String) -> String {
    if text.contains("[Archived]") {
        text.strip_suffix("[Archived]").unwrap().to_string()
    } else {
        text
    }
}
